<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网页刮刮乐</title>
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            margin: 0;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            font-family: 'Arial', sans-serif;
        }
        
        .container {
            text-align: center;
            padding: 20px;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            margin-bottom: 30px;
        }
        
        h1 {
            color: #333;
            margin-bottom: 20px;
        }
        
        .scratch-card {
            position: relative;
            margin: 0 auto;
            width: 400px;
            height: 300px;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
        }
        
        .prize-image {
            width: 100%;
            height: 100%;
            object-fit: cover;
            border-radius: 12px;
        }
        
        #canvas {
            position: absolute;
            top: 0;
            left: 0;
            border-radius: 12px;
            cursor: pointer;
        }
        
        .controls {
            margin-top: 20px;
            display: flex;
            gap: 15px;
        }
        
        button {
            padding: 12px 25px;
            background: #4e54c8;
            color: white;
            border: none;
            border-radius: 30px;
            cursor: pointer;
            font-size: 16px;
            font-weight: bold;
            transition: all 0.3s ease;
            box-shadow: 0 4px 10px rgba(78, 84, 200, 0.3);
        }
        
        button:hover {
            background: #3f43a8;
            transform: translateY(-2px);
            box-shadow: 0 6px 15px rgba(78, 84, 200, 0.4);
        }
        
        .scratch-percentage {
            margin-top: 15px;
            font-size: 18px;
            color: #555;
        }
        
        .instructions {
            margin-top: 25px;
            color: #666;
            font-size: 16px;
            text-align: center;
            max-width: 500px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>网页刮刮乐</h1>
        
        <div class="scratch-card">
            <img class="prize-image" src="https://images.unsplash.com/photo-1512917774080-9991f1c4c750?auto=format&fit=crop&w=400&h=300&q=80" alt="奖品图片">
            <canvas id="canvas" width="400" height="300"></canvas>
        </div>
        
        <div class="scratch-percentage" id="percentage">已刮开: 0%</div>
        
        <div class="controls">
            <button id="resetBtn">重新开始</button>
            <button id="revealBtn">全部揭开</button>
        </div>
        

    </div>

    <script>
        const canvas = document.getElementById('canvas');
        const context = canvas.getContext('2d');
        const resetBtn = document.getElementById('resetBtn');
        const revealBtn = document.getElementById('revealBtn');
        const percentageEl = document.getElementById('percentage');
        
        // 初始化涂层
        function initScratchLayer() {
            context.fillStyle = "#8f94fb";
            context.fillRect(0, 0, 400, 300);
            
            // 添加一些图案到涂层上
            context.font = "bold 40px Arial";
            context.fillStyle = "rgba(255, 255, 255, 0.7)";
            context.textAlign = "center";
            context.fillText("刮开有奖", 200, 150);
            
            context.font = "20px Arial";
            context.fillText("试试手气吧!", 200, 190);
            
            // 重置百分比
            scratchedPixels = 0;
            totalPixels = 400 * 300;
            updatePercentage();
        }
        
        // 初始化变量
        let isScratching = false;
        let scratchedPixels = 0;
        let totalPixels = 400 * 300;
        
        // 计算刮开百分比
        function updatePercentage() {
            const percentage = Math.round((scratchedPixels / totalPixels) * 100);
            percentageEl.textContent = `已刮开: ${percentage}%`;
            
            // 如果刮开超过70%，自动全部清除
            if (percentage > 70) {
                revealAll();
            }
        }
        
        // 全部揭开
        function revealAll() {
            context.clearRect(0, 0, 400, 300);
            scratchedPixels = totalPixels;
            updatePercentage();
        }
        
        // 鼠标事件处理
        canvas.addEventListener('mousedown', () => {
            isScratching = true;
        });
        
        canvas.addEventListener('mouseup', () => {
            isScratching = false;
        });
        
        canvas.addEventListener('mousemove', (e) => {
            if (isScratching) {
                const rect = canvas.getBoundingClientRect();
                const x = e.clientX - rect.left;
                const y = e.clientY - rect.top;
                
                context.globalCompositeOperation = "destination-out";
                context.beginPath();
                context.arc(x, y, 20, 0, Math.PI * 2);
                context.fill();
                
                // 更新刮开的像素计数（简化估算）
                scratchedPixels += 100;
                updatePercentage();
            }
        });
        
        // 触摸事件支持
        canvas.addEventListener('touchstart', (e) => {
            e.preventDefault();
            isScratching = true;
        });
        
        canvas.addEventListener('touchend', () => {
            isScratching = false;
        });
        
        canvas.addEventListener('touchmove', (e) => {
            if (isScratching) {
                const rect = canvas.getBoundingClientRect();
                const touch = e.touches[0];
                const x = touch.clientX - rect.left;
                const y = touch.clientY - rect.top;
                
                context.globalCompositeOperation = "destination-out";
                context.beginPath();
                context.arc(x, y, 20, 0, Math.PI * 2);
                context.fill();
                
                // 更新刮开的像素计数（简化估算）
                scratchedPixels += 100;
                updatePercentage();
            }
        });
        
        // 按钮事件
        resetBtn.addEventListener('click', initScratchLayer);
        revealBtn.addEventListener('click', revealAll);
        
        // 初始化
        initScratchLayer();
    </script>
</body>
</html>